var api = "http://baoyuesi.com.cn:8080/";
var url = this.GetRequest();

var userId = getCookie("userId");

// var script = document.createElement("script");
// script.type = "text/javascript";
// script.src = "https://blog-static.cnblogs.com/files/chenzxl/sha1.js";
// document.body.appendChild(script);

window.onresize = setHtmlFontSize();

function toastText(t) {
  $("body").append('<p class="toastText"><span>' + t + "</span></p>");

  setTimeout(() => {
    $(".toastText").remove();
  }, 2000);
}

function setHtmlFontSize() {
  var htmlDom = document.getElementsByTagName("html")[0];
  var htmlWidth =
    document.documentElement.clientWidth || document.body.clientWidth;
  if (htmlWidth >= 750) {
    htmlWidth = 750;
  }
  if (htmlWidth <= 320) {
    htmlWidth = 320;
  }
  htmlDom.style.fontSize = htmlWidth / 3.75 + "px";
  //1rem = 100px
}

function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
      break;
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
      break;
    default:
      return 0;
      break;
  }
}

function getCookie(name) {
  let arr,
    reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  arr = document.cookie.match(reg);
  if (arr == undefined) {
    return null;
  }
  return unescape(arr[2]);
}

function setCookie(name, value) {
  var Days = 1;
  var exp = new Date();
  //console.log(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  //console.log(exp);
  //console.log(exp.toGMTString());
  document.cookie = name + "=" + escape(value) + ";expires=" + exp;
}

function GetRequest() {
  var url = location.search; //获取url中"?"符后的字串
  var theRequest = new Object();
  if (url.indexOf("?") != -1) {
    var str = url.substr(1);
    var strs = str.split("&");
    for (var i = 0; i < strs.length; i++) {
      theRequest[strs[i].split("=")[0]] = decodeURIComponent(
        strs[i].split("=")[1]
      );
    }
  }
  return theRequest;
}

// noncestr
function createNonceStr() {
  return Math.random().toString(36).substr(2, 15);
}

// timestamp
function createTimeStamp() {
  return parseInt(new Date().getTime() / 1000) + "";
}

// 计算签名方法
function calcSignature(ticket, noncestr, ts, url) {
  var str =
    "jsapi_ticket=" +
    ticket +
    "&noncestr=" +
    noncestr +
    "&timestamp=" +
    ts +
    "&url=" +
    url;
  shaObj = sha1(str);
  return shaObj;
}
//var signature = calcSignature(ticket, noncestr, timestamp, url);
// 　hex_sha1.hex_sha1(str)

function share(i) {
  console.log(i);

  var url = window.location.href.split("#")[0]; //window.location.href;

  var title = "";
  var desc = "";
  var img = "";
  var link = "";

  if (i == undefined) {
    title = "分享首页";
    desc = "分享首页";
    img = "http://www.baoyuesi.com.cn/img/si.jpg";
    link = "http://www.baoyuesi.com.cn/";
  } else {
    title = "分享详情";
    desc = "分享详情";
    img = "http://www.baoyuesi.com.cn/img/si.jpg";
    link = "http://www.baoyuesi.com.cn/share.html?recordId=" + i;
  }
  // console.log(url)
  url = encodeURIComponent(url);
  // console.log(url)

  jQuery.get(api + "/app/goodMan/getWxThing?obj=" + url, function (res) {
    if (res.code == 200) {
      var d = res.data;

      var timestamp = String(d.timestamp); //createTimeStamp();
      var nonceStr = d.nonceStr; //createNonceStr();

      var signature = d.sign; //calcSignature(d.ticket, createNonceStr(), createTimeStamp(), url);
      console.log(timestamp);
      console.log(nonceStr);
      console.log(url);

      //分享
      wx.config({
        debug: false,
        appId: d.appid,
        timestamp: timestamp,
        nonceStr: nonceStr, //d.nonceStr,
        signature: signature, //d.sign,
        jsApiList: [
          "checkJsApi",
          "onMenuShareWeibo",
          "updateAppMessageShareData",
          "updateTimelineShareData",
          "onMenuShareTimeline",
          "onMenuShareAppMessage",
          "onMenuShareQQ",
          "onMenuShareWeibo",
          "onMenuShareQZone",
          "startRecord",
          "stopRecord",
          "onVoiceRecordEnd",
          "playVoice",
          "pauseVoice",
          "stopVoice",
          "onVoicePlayEnd",
          "uploadVoice",
          "downloadVoice",
          "chooseImage",
          "previewImage",
          "uploadImage",
          "downloadImage",
          "chooseWXPay",
          "hideMenuItems",
          "openLocation",
          "getLocation",
          "showAllNonBaseMenuItem",
          "hideToolbar",
        ],
        success: function (res) {
          console.log("configsuccess", res);
        },
        fail: function (res) {
          console.log(res);
          if (res.errMsg.indexOf("function not exist") > -1) {
            alert("版本过低请升级");
          }
        },
      });
      var wx_title = title;
      var wx_link = link;
      var wx_img = img;
      var wx_desc = desc;

      wx.checkJsApi({
        jsApiList: ["updateAppMessageShareData"], // 需要检测的JS接口列表，所有JS接口列表见附录2,
        success: function (res) {
          // alert("可用");
          console.log("可用");
        },
        fail: (err) => {
          alert("不可用");
          console.log(err, "不可用");
        },
      });

      wx.ready(function () {
        // alert("ready");

        wx.updateAppMessageShareData({
          title: wx_title,
          desc: wx_desc,
          link: wx_link,
          imgUrl: wx_img,
          success: function () {
            // 设置成功
            console.log("成功1");
            //that.toast_msg();
          },
        });

        wx.updateTimelineShareData({
          title: wx_title,
          link: wx_link,
          imgUrl: wx_img,
          success: function () {
            // 设置成功
            console.log("成功2");
          },
        });
      }),
        wx.error(function (res) {
          console.log(res);
        });
    }
  });
}
